#!/usr/bin/env bash
#set -x
SELFDIR=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)

# $1 install
# $2 <version>
function main()
{
    if [[ $# -lt 2 ]]; then
        return 1
    fi

    if [[ ! -d "${PKGREPO}" ]]; then
        echo    "The required environment variable \${PKGREPO} is missing"
        return  1
    fi

    if [[ ! -d "${DEVREPO}" ]]; then
        echo    "The required environment variable \${DEVREPO} is missing"
        return  1
    fi

    #   载入模块初始化
    source "${PKGREPO}/.share/module-install-init.bash" "$@"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "The required environment variable \${DEVREPO} is missing"
        return  1
    fi

    #   确定主包和主包内部目录
    local majorpkg=$(cat "${MODULE_PACKAGE_DIR}/VERSION" | grep PACKAGE | awk '{print $3}')
    local innerdir=$(cat "${MODULE_PACKAGE_DIR}/VERSION" | grep PACKAGE | awk '{print $4}')

    #   备份旧软件、创建构建目录、解压主软件包
    framework backup      &&  \
    framework maketransit &&  \
    framework unzip     "${MODULE_PACKAGE_DIR}/${majorpkg}"    "${MODULE_TRANSIT_DIR}"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    ""
        return  1
    fi

    #   Qt 的编译环境不支持软连接
    export MODULE_TRANSIT_DIR=$(realpath -- "${MODULE_TRANSIT_DIR}")

    #   如果设置了 Qt6_DIR 会导致 qt 默认往旧的位置安装，所以需要禁用掉
    unset Qt6_DIR

    #   Qt 的部分工具对 glibc 的版本有依赖
    if [[ -d "${GCC_HOME}" ]]; then
        export LD_LIBRARY_PATH="${GCC_HOME}/lib64:${LD_LIBRARY_PATH}"
    fi

    #   编译和安装
    local builddir="${MODULE_TRANSIT_DIR}/${innerdir}"
    mkdir       -p "${builddir}/build"
    mkdir       -p "${MODULE_INSTALL_DIR}"              &&  \
    cd          "${builddir}"                           &&  \
    ./configure --prefix="${MODULE_INSTALL_DIR}"        \
                -shared                                 \
                -debug                                  \
                \
                -skip qtwebengine                       \
                -skip qtwebchannel                      \
                -skip qtwebview						    \
                -skip qtwebsockets                      \
                \
                -skip qtwayland						    \
                -skip qtlottie						    \
                \
                -skip qtserial						    \
                -skip qtserialbus						\
                -skip qtserialport                      \
                \
                -skip qt3d						        \
                -skip qtgraphs                          \
                -skip qtcharts                          \
                -skip qtdatavis3d                       \
                \
                -skip qtmqtt                            \
                -skip qtpositioning                     \
                -skip qtlocation                        \
                -skip qtgrpc                            \
                -skip qtvirtualkeyboard                 \
                \
                -skip qtspeech                          \
                -skip qt5compat                         \
                -skip qtactiveqt                        \
                -skip qtsvg                             \
                -skip qtcoap                            \
                -skip qtsensors                         \
                -skip qtconnectivity                    \
                -skip qtscxml                           \
                -skip qtmultimedia                      \
                    -skip qtdoc                         \
                    -skip qtshadertools                 \
                        -skip qtdeclarative             \
                            -skip qtquicktimeline       \
                            -skip qtquick3d             \
                            -skip qtquick3dphysics      \
                            -skip qtopcua               \
                    -skip qtquickeffectmaker            \
                -skip qthttpserver                      \
                -skip qtremoteobjects                   \
                -skip qtnetwork                         \
                -skip qtlanguageserver                  \
                -skip qtnetworkauth                     \
                \
                -xcb                                    \
                -xcb-xlib                               \
                -xkbcommon                              \
                -no-feature-sqlmodel                    \
                -no-feature-qtpdf-build                 \
                -no-feature-qtpdf-quick-build           \
                -no-feature-qtpdf-widgets-build         \
                -no-feature-qtwebengine-build           \
                -no-feature-qtwebengine-core-build      \
                -no-feature-qtwebengine-quick-build     \
                -no-feature-qtwebengine-widgets-build   \
                -no-feature-webengine-developer-build   \
                -no-feature-webengine-embedded-build    \
                -no-feature-webengine-jumbo-build       \
                -no-feature-designer                    \
                -no-feature-assistant                   \
                                                        &&  \
    cd          "${builddir}/build"                     &&  \
    cmake       --build .. --parallel                   &&  \
    cmake       --install ..                            &&  \
    echo        "Build and install '${MODULE_LABEL}' success"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Build and install '${MODULE_LABEL}' failed(${RESULT}): '${MODULE_TRANSIT_DIR}'"
        return  1
    fi

    #   写 setup 文件
    touch "${MODULE_INSTALL_DIR}/setup.bash"
    echo "export ${MODULE_NAME_UPPER}_HOME=\"\${DEVREPO}/${MODULE_LABEL}\""     >>   "${MODULE_INSTALL_DIR}/setup.bash"
    echo "export PATH=\"\${${MODULE_NAME_UPPER}_HOME}/bin:\${PATH}\""           >>   "${MODULE_INSTALL_DIR}/setup.bash"
    #
    touch "${MODULE_INSTALL_DIR}/setup.csh"
    echo "setenv ${MODULE_NAME_UPPER}_HOME \"\${DEVREPO}/${MODULE_LABEL}\""     >>   "${MODULE_INSTALL_DIR}/setup.csh"
    echo "setenv PATH \"\${${MODULE_NAME_UPPER}_HOME}/bin:\${PATH}\""           >>   "${MODULE_INSTALL_DIR}/setup.csh"

    return 0
}

main    "$@"
exit    "$?"
